package com.study.util;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

public class DateUtil {

	private static void notNull(Object... obj) {
		for (Object object : obj) {
			if (Objects.isNull(object)) {
				throw new IllegalArgumentException("参数不能为空！");
			}
		}
	}

	/**
	 * 将日期类型格式化成字符串
	 * 
	 * @param date    日期
	 * @param pattern 规则
	 * @return 符合规则的字符串日期
	 */
	public static String date2Str(Date date, String pattern) {
		notNull(date, pattern);
		LocalDateTime instant = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
		return dtf.format(instant);
	}

	/**
	 * 格式化Timestamp时间
	 * 
	 * @param timestamp Timestamp类型时间
	 * @param pattern   规则
	 * @return 符合规则的字符串日期
	 */
	public static String timestamp2Str(Timestamp timestamp, String pattern) {
		notNull(timestamp, pattern);
		LocalDateTime instant = LocalDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
		return dtf.format(instant);
	}

	/**
	 * 将日期字符串格式化成日期类型
	 * 
	 * @param dateStr 日期字符串
	 * @param pattern 规则
	 * @return Date类型日期
	 */
	public static Date str2Date(String dateStr, String pattern) {
		notNull(dateStr, pattern);
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
		LocalDateTime ldt = LocalDateTime.parse(dateStr, dtf);
		Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
		return Date.from(instant);
	}

	/**
	 * 转sql的日期格式
	 * 
	 * @param dateStr 日期字符串
	 * @param pattern 规则
	 * @return sql的日期
	 */
	public static java.sql.Date str2SqlDate(String dateStr, String pattern) {
		Date date = str2Date(dateStr, pattern);
		java.sql.Date dateTime = new java.sql.Date(date.getTime());
		return dateTime;
	}

	/**
	 * 转sql的时间格式
	 * 
	 * @param dateStr 日期字符串
	 * @param pattern 规则
	 * @return sql的日期
	 */
	public static java.sql.Timestamp str2SqlTime(String dateStr, String pattern) {
		Date date = str2Date(dateStr, pattern);
		java.sql.Timestamp time = new java.sql.Timestamp(date.getTime());
		return time;
	}

	/**
	 * 计算两个日期之差
	 * 
	 * @param d1 d1
	 * @param d2 d2
	 * @return 两date相差毫秒值
	 */
	public static Long calDiff(Date d1, Date d2) {
		notNull(d1, d2);
		return Math.abs(d1.getTime() - d2.getTime());
	}

	/**
	 * 精确到时分秒
	 * 
	 * @param startDate 开始日期
	 * @param endDate   结束日期
	 * @return 时间差
	 */
	public static String calBetweenTime(Date startDate, Date endDate) {

		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		long ns = 1000;

		long diff = calDiff(startDate, endDate);
		long day = diff / nd;
		long hour = diff % nd / nh;
		long min = diff % nd % nh / nm;
		long sec = diff % nd % nh % nm / ns;
		String str = day + "天" + hour + "小时" + min + "分钟" + sec + "秒";

		if (day == 0) {
			if (hour != 0) {
				str = hour + "小时" + min + "分钟" + sec + "秒";
			} else {
				if (min != 0) {
					str = min + "分钟" + sec + "秒";
				} else {
					if (sec != 0) {
						str = +sec + "秒";
					}
				}
			}
		}
		return str;
	}

	public static void main(String[] args) {

		Timestamp t = new Timestamp(System.currentTimeMillis());
		System.out.println(date2Str(t, "yyyy-MM-dd HH:mm:ss"));

		Date date = str2Date("2022-05-30 23:59:02", "yyyy-MM-dd HH:mm:ss");
		Date date1 = str2Date("2022-05-30 00:00:02", "yyyy-MM-dd HH:mm:ss");

		String poor = calBetweenTime(date1, date);
		System.out.println(poor);
	}

}
